package be.nabu.vfs.provider;


import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;

import be.nabu.vfs.provider.api.ArchiveFactory;

public class SPIArchiveResolver implements ArchiveResolver {
	
	private static ServiceLoader<ArchiveFactory> serviceLoader = ServiceLoader.load(ArchiveFactory.class);

	private Map<String, ArchiveFactory> resolvedLoaders = new HashMap<String, ArchiveFactory>();
	
	@Override
	public ArchiveFactory getArchiveFactory(String contentType) {
		if (!resolvedLoaders.containsKey(contentType)) {
			ArchiveFactory correctFactory = null;
			for (ArchiveFactory factory : serviceLoader) {
				if (factory.getSupportedContentTypes().contains(contentType)) {
					correctFactory = factory;
					break;
				}
			}
			resolvedLoaders.put(contentType, correctFactory);
		}
		return resolvedLoaders.get(contentType);
	}

}
